﻿@using RooHui.Infrastructure.Entities.Enum
@using RooHui.Tool
@{
    ViewBag.Title = "ProjectInvestment";
    Layout = "~/Views/Shared/_Layout.cshtml";
    RooHui.Portal.Biz.Model.ProjectDetailModel project = ViewBag.ProjectDetail;
    RooHui.Infrastructure.Entities.Entity.Account account = ViewBag.Account;
}
@section head{
    <link rel="stylesheet" type="text/css" href="/content/plugins/fancybox/jquery.fancybox-1.3.4.css" />
    <style type="text/css">
        table.project tr td.title
        {
            width: 25%;
            text-align: right;
            font-size: 16px;
            color: #666;
        }
        table.project tr td.filed
        {
            width: 25%;
            text-align: right;
            font-size: 18px;
        }
        .wall
        {
            padding: 10px;
            border: 1px solid #e3e3e3;
            text-shadow: 0 1px 0 #fff;
            border-radius: 4px;
            -webkit-border-radius: 4px;
            -moz-border-radius: 4px;
            -moz-box-sizing: border-box;
            -webkit-box-sizing: border-box;
            -ms-box-sizing: border-box;
        }
        hr
        {
            margin: 10px 0;
        }
    </style>
}
<div class="container" style="padding-top: 20px;">
    <div class="bg-light wall">
        <div class="row-fluid">
            <div class="span8">
                <a href="javascript:;" class="blue" style="font-size: 24px;">@project.ProductName @project.ProjectTitle</a>
                <span style="color: #777; font-size: 12px;">项目编码：@project.ProjectCode</span><br />
                项目融资金额：<span style="color: #e86e00;">￥ @(Math.Round(project.Financing / 10000, 2))
                    万元</span> ， 还需融资额：<span style="color: #3498DB;">￥
                        @(Math.Round(project.RemainFinancing / 10000, 2))
                        万元</span>
                <hr />
                <table class="project" style="width: 90%;">
                    <tr>
                        <td class="title">
                            预期年化利率：
                        </td>
                        <td class="filed">
                            <span class="poptag" style="border-bottom: 1px dashed #333; color: #e86e00;">
                                @(project.Policies.FirstOrDefault(c => !c.IsAttach).Rate)
                                % + 奖励 </span>
                        </td>
                        <td class="title">
                            担保公司：
                        </td>
                        <td class="filed">
                            <img style="width:20px;height:20px;" alt="@project.GuaranteeCompanyName" src="@project.GuaranteeCompanyLogo">
                            @project.GuaranteeCompanyName
                        </td>
                    </tr>
                    <tr>
                        <td class="title">
                            获取收益方式：
                        </td>
                        <td class="filed">
                            <span style="border-bottom: 1px dashed #333;">@project.Policies.FirstOrDefault(c => !c.IsAttach).BalanceType.Mapping()</span>
                        </td>
                        <td class="title">
                            融资期限：
                        </td>
                        <td class="filed">
                            @(project.ProjectCycleAmount)
                            期&nbsp;&nbsp;&nbsp;(@project.Policies.FirstOrDefault(c => !c.IsAttach).Duration
                            @(project.Policies.FirstOrDefault(c => !c.IsAttach).DurationType.Mapping())/期)
                        </td>
                    </tr>
                    <tr>
                        <td class="title">
                            手续费：
                        </td>
                        <td class="filed">
                            0.00 元
                        </td>
                        <td class="title">
                            @(((int)project.ProjectStatus < (int)ProjectStatus.Repayment) ? "(预计)" : "")起息日：
                        </td>
                        <td class="filed">@project.BeginDate.ToString("yyyy-MM-dd")
                        </td>
                    </tr>
                    <tr>
                        <td class="title">
                            融资进度：
                        </td>
                        <td colspan="3">
                            @{
                                var percent = (int)(((project.Financing - project.RemainFinancing) / project.Financing) * 100);
                                var progressStyle = percent <= 50 ? "progress-info" : percent < 100 ? "progress-warning" : "progress-success";
                            }
                            <div class="progress @progressStyle progress-striped" style="margin:0;">
                                <div class="bar" style="width: @percent%">
                                </div>
                            </div>
                        </td>
                    </tr>
                </table>
                <hr />
                <div class="row-fluid">
                    @if (project.Policies.Any(c => c.IsAttach))
                    {
                        <div class="span5">
                            <ul>
                                <li><strong style="color: Red; font-size: 20px;">奖励：</strong></li>
                                @{
                        var policies = project.Policies.Where(c => c.IsAttach).ToList();
                        foreach (var policy in policies)
                        {
                                    <li><strong style="color: Red;">额外追加 @policy.Rate @(policy.SettlementType == SettlementType.Proportion ? "% 年化收益率" : "固定收益")
                                        (@policy.CycleAmount 期 @policy.Duration @policy.DurationType.Mapping()/期)</strong></li>
                        }
                                }
                            </ul>
                        </div>}@{
                            List<RooHui.Infrastructure.Entities.Entity.PromotionPolicy> list = ViewBag.PromotionPolicies;
                            if (list != null && list.Any())
                            {
                            <div class="span7">
                                <ul>
                                    <li><strong style="color: Green; font-size: 20px;">促销：</strong>（注：仅在第一次投资本项目时检查是否符合促销条件）</li>
                                    @foreach (var item in list)
                                    {
                                        <li><strong style="color: Green;">@item.Summary</strong></li>
                                    }
                                </ul>
                            </div>
                            }
                        }
                </div>
            </div>
            <div class="span4">
                <div class="headline headline-md">
                    <h2>
                        合同协议</h2>
                    <ul class="list-unstyled blog-photos" style="margin: 10px 0;">
                        @{
                            var agreement = project.Resource.Where(c => c.ResouceSubType == ResourceSubType.ProjectAgreement).Take(6).ToList();
                            var description = project.Resource.Where(c => c.ResouceSubType == ResourceSubType.ProjectDesction).Take(6).ToList();
                            foreach (var item in agreement)
                            {
                                if (item.PrivateLevel == PrivateLevel.Public ||
                                    (item.PrivateLevel == PrivateLevel.MemberOnly && HttpContext.Current.User.Identity.IsAuthenticated))
                                {
                            <li><a class="contract" href="@item.Path" title="@item.Name">
                                <img class="hover-effect" alt="@item.Name" src="@item.Path"></a></li>
                                }
                            }
                        }
                    </ul>
                </div>
            </div>
        </div>
    </div>
    <div class="row-fluid">
        <div class="span12">
            <div class="wall @(description.Any()?"span8":"")">
                <h3>
                    投资</h3>
                <hr />
                <form id="frminvestment" action="/project/projectinvestment" method="post">
                <input type="hidden" name="token" value="@ViewBag.Token" />
                <input type="hidden" name="projectId" value="@project.ProjectId" />
                <input type="hidden" id="hidincreaseMoney" value="@project.IncreaseMoney" />
                @if (!String.IsNullOrEmpty(ViewBag.Message))
                {
                    <div class="alert alert-error">@ViewBag.Message</div>
                }
                <table style="width: 100%;">
                    <tr style="height: 48px; line-height: 48px;">
                        <td style="width: 13%; text-align: center;">
                            最大投资额：
                        </td>
                        <td>
                            ￥ <strong id="maxrange">@ViewBag.MyRemainInvestAmount</strong> （本项目单人最大投资额：@project.MaxInvestment）
                        </td>
                    </tr>
                    <tr style="height: 48px; line-height: 48px;">
                        <td style="text-align: center;">
                            账户余额：
                        </td>
                        <td class="orange">
                            ￥ <b id="balancebox">@account.BalanceAmount</b>
                            <input type="button" style="margin-left: 20px;" value="刷新" class="btn-u" onclick="refrashAccount(this)" />
                            <a href="/member/recharge" target="_blank">
                                <input type="button" value="充值" class="btn-u btn-u-orange" />
                            </a>
                        </td>
                    </tr>
                    <tr style="height: 48px; line-height: 48px;">
                        <td style="text-align: center;">
                            投资金额：
                        </td>
                        <td>
                            <input type="text" id="txtinvest" name="invest" value="@project.IncreaseMoney" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                        </td>
                        <td>
                            投资单份金额：@project.IncreaseMoney 元
                        </td>
                    </tr>
                    <tr style="height: 46px; line-height: 46px;">
                        <td colspan="2" style="padding-left: 35px;">
                            @if (ViewBag.MyRemainInvestAmount <= 0)
                            {
                                <input type="button" class="btn-u btn-u-default" disabled="disabled" value="投资已达限额" />
                            }
                            else
                            {
                                <input type="button" class="btn-u" value="确定投资" onclick="doInvest()" />
                            }
                            <input type="button" class="btn-u btn-u-red" value="收益计划" onclick="getInvestPlan(this)" />
                        </td>
                    </tr>
                </table>
                </form>
            </div>
            @if (description.Any())
            {
            <div class="wall span4">
                <div class="headline headline-md">
                    <h2>
                        其他资料</h2>
                    <ul class="list-unstyled blog-photos" style="margin: 10px 0;">
                        @{
                foreach (var item in description)
                {
                    if (item.PrivateLevel == PrivateLevel.Public ||
                        (item.PrivateLevel == PrivateLevel.MemberOnly && HttpContext.Current.User.Identity.IsAuthenticated))
                    {
                            <li><a class="contract" href="@item.Path" title="@item.Name">
                                <img class="hover-effect" alt="@item.Name" src="@item.Path"></a></li>
                    }
                }
                        }
                    </ul>
                </div>
            </div>
            }
        </div>
        
    </div>
    <div class="row-fluid">
        <div class="span12">
            <div class="wall" style="margin-top: 10px; min-height: 220px;">
                <h3>
                    预期收益</h3>
                <hr />
                <p>
                    以投资<span id="increaseMoney">@(project.IncreaseMoney)</span>为例，@(((int)project.ProjectStatus < (int)ProjectStatus.Repayment) ? "(预计)" : "")起息日为：@project.BeginDate.ToString("yyyy-MM-dd")</p>
                <table class="table table-bordered">
                    <thead table-bordered>
                        <tr>
                            <th width="30%">
                                预期还款时间
                            </th>
                            <th>
                                类型
                            </th>
                            <th width="30%">
                                还款金额(元)
                            </th>
                        </tr>
                    </thead>
                    <tbody id="planbox">
                        @{
                            List<IGrouping<DateTime, RooHui.Portal.Biz.Model.PayBackItemModel>> data = ViewBag.Plan;
                            foreach (var item in data)
                            {
                                for (int i = 0, len = item.Count(); i < len; i++)
                                {
                                    var paybackItem = item.ToList();
                            <tr>
                                @if (i == 0)
                                {
                                    <td rowspan="@paybackItem.Count()">@paybackItem[i].PayBackDate.ToString("yyyy-MM-dd")
                                    </td>
                                }
                                <td>@paybackItem[i].Name
                                </td>
                                <td>
                                    ￥ @paybackItem[i].Value（元）
                                </td>
                            </tr> 
                                }
                            }
                        }
                        <tr>
                            <td>
                                &nbsp;
                            </td>
                            <td colspan="2" style="color: #e86e00; font-size: 20px; text-align: right;">
                                <strong>合计收益： ￥ @data.Sum(c => c.Sum(i => i.Value)) 元</strong>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
@section scripts{
    <script type="text/javascript" src="/content/plugins/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
    <script type="text/javascript">
        $(function () {
            $('.contract').fancybox();
            $('#labproject').addClass('active');
        });
        function doInvest() {
            var max = $('#maxrange').text();
            var b = $('#balancebox').text();
            var iv = $('#txtinvest').val();
            var increase = $('#hidincreaseMoney').val();
            if (parseFloat(max) < parseFloat(iv)) {
                if ($('#txtinvest').siblings('span').length != 0) {
                    $('#txtinvest').siblings('span').remove();
                }
                $('#txtinvest').css({ 'border-color': '#b94a48' }).after('<span style="color:#b94a48;"> 您在本项目最多还能投资：￥' + max + '</span>');
                return;
            }
            if (parseFloat(iv) > parseFloat('@project.RemainFinancing')) {
                if ($('#txtinvest').siblings('span').length != 0) {
                    $('#txtinvest').siblings('span').remove();
                }
                $('#txtinvest').css({ 'border-color': '#b94a48' }).after('<span style="color:#b94a48;"> 投资额不能大于项目剩余总融资额</span>');
                return;
            }
            if (!(/^\d+\.\d{1,2}$/.test(b) && /^\d+\.?\d*$/.test(iv))) {
                if ($('#txtinvest').siblings('span').length != 0) {
                    $('#txtinvest').siblings('span').remove();
                }
                $('#txtinvest').css({ 'border-color': '#b94a48' }).after('<span style="color:#b94a48;"> 投资金额输入有误</span>');
                return;
            }
            else if (parseFloat(b) < parseFloat(iv)) {
                if ($('#txtinvest').siblings('span').length != 0) {
                    $('#txtinvest').siblings('span').remove();
                }
                $('#txtinvest').css({ 'border-color': '#b94a48' }).after('<span style="color:#b94a48;"> 您的余额不足，请先充值</span>');
                return;
            }
            else if (parseFloat(iv) <= 0 || parseFloat(iv) % increase != 0) {
                if ($('#txtinvest').siblings('span').length != 0) {
                    $('#txtinvest').siblings('span').remove();
                }
                $('#txtinvest').css({ 'border-color': '#b94a48' }).after('<span style="color:#b94a48;"> 投资金额必须为单份金额的整数倍</span>');
                return;
            }
            else {
                $('#txtinvest').css({ 'border-color': '#ccc' }).siblings('span').remove();
                $('#frminvestment').submit();
            }

        }
        function refrashAccount(sender) {
            $.ajax({
                url: '/account/getAccountInfo',
                data: {
                    u: '@HttpContext.Current.User.Identity.Name'
                },
                cache: false,
                beforeSend: function () {
                    $(sender).attr('disabled', 'disabled').parent().children(':last').after('<i class="loading"></i>');
                },
                success: function (result) {
                    $(sender).siblings('i').remove();
                    $(sender).siblings('b').text(result);
                    $(sender).removeAttr('disabled');
                }
            });
        }
        function getInvestPlan(sender) {
            $.ajax({
                url: '/project/getInvestPlan',
                data: {
                    projectId: '@project.ProjectId',
                    invest: $('#txtinvest').val()
                },
                beforeSend: function () {
                    $('#planbox').html('');
                    $('#increaseMoney').text($('#txtinvest').val());
                    $(sender).removeClass('btn-u-red').addClass('btn-u-default').attr('disabled', 'disabled').after('<i class="loading"></i>');
                },
                success: function (result) {
                    $(sender).removeClass('btn-u-default').addClass('btn-u-red')
                        .removeAttr('disabled');
                    $(sender).siblings('i').remove();
                    var list = eval('(' + result + ')');
                    if (list && list.length > 0) {
                        for (var i = 0, len = list.length; i < len; i++) {
                            for (var j = 0, jlen = list[i].length; j < jlen; j++) {
                                var s = '', datetime = '';
                                datetime = list[i][j].PayBackDate.substring(0, list[i][j].PayBackDate.indexOf('T'));
                                if (list[i].length > 1) {
                                    if (j == 0) {
                                        $('#planbox').append($('<tr><td rowspan = "' + list[i].length + '">' + datetime + '</td><td>' + list[i][j].Name + '</td><td>￥ ' + list[i][j].Value + '（元）</td></tr>'));
                                    } else {
                                        $('#planbox').append($('<tr><td>' + list[i][j].Name + '</td><td>￥ ' + list[i][j].Value + '（元）</td></tr>'));
                                    }
                                }
                                else {
                                    $('#planbox').append($('<tr><td>' + datetime + '</td><td>' + list[i][j].Name + '</td><td>￥ ' + list[i][j].Value + '（元）</td></tr>'));
                                }
                            }
                        }
                        $('#planbox').append($('<tr><td>&nbsp;</td><td colspan="2" style="color: #e86e00; font-size: 20px; text-align: right;"><strong>合计收益： ￥ ' + getsum(list) + ' 元</strong></td></tr>'));
                    }
                }
            });
        }
        function getsum(list) {
            var val = 0;
            for (var i = 0, ilen = list.length; i < ilen; i++) {
                for (var j = 0, jlen = list[i].length; j < jlen; j++) {
                    val += parseFloat(list[i][j].Value, 10);
                }
            }
            return Math.round(val * 100) / 100;
        }
    </script>
}
